/*
 * A Gneral purpose java based Math Library created by Susanta Tewari.
 * Copyright is protected. Contact Susanta Tewari at <statsusant@yahoo.com>
 */

package org.math.data.storage;

/**
 *
 * @author Susanta Tewari <stewari@yahoo.com>
 */
public class IntegerStorage extends Storage {

    /** Field description */
    private int val = 0;

    /**
     * Constructs ...
     *
     * @param val description
     */
    public IntegerStorage(int val) {

        super(StorageType.DOUBLE);

        this.val = val;
    }

    /**
     * Constructs ...
     */
    public IntegerStorage() {
        this(0);
    }

    int getVal() {
        return val;
    }

    /**
     * @throws ClassCastException
     */
    @Override
    public Storage plus(Storage storageType) {

        IntegerStorage storage = (IntegerStorage) storageType;

        return new IntegerStorage(this.val + storage.getVal());
    }

    @Override
    public Storage minus(Storage storageType) {

        IntegerStorage storage = (IntegerStorage) storageType;

        return new IntegerStorage(this.val - storage.getVal());
    }

    @Override
    public Storage times(Storage storageType) {

        IntegerStorage storage = (IntegerStorage) storageType;

        return new IntegerStorage(this.val * storage.getVal());
    }

    @Override
    public Storage inverse() {
        return new DoubleStorage(1.0 / this.getVal());
    }

    @Override
    public Storage divide(Storage storageType) {

        IntegerStorage storage = (IntegerStorage) storageType;

        return new DoubleStorage((double) this.val / storage.getVal());
    }

    @Override
    public Storage doubleStorage() {
        return new DoubleStorage(this.getVal());
    }

    @Override
    public Storage floatStorage() {
        return new FloatStorage(this.getVal());
    }

    @Override
    public Storage intStorage() {
        return this;
    }

    @Override
    public Storage longStorage() {
        return new LongStorage(this.getVal());
    }

    @Override
    public Storage shortStorage() {
        return new ShortStorage((short) this.getVal());
    }

    @Override
    public Storage copy() {
        return new IntegerStorage(this.val);
    }

    @Override
    public short shortValue() {
        return (short) this.val;
    }

    @Override
    public double doubleValue() {
        return (double) this.val;
    }

    @Override
    public long longValue() {
        return (long) this.val;
    }

    @Override
    public float floatValue() {
        return (float) this.val;
    }

    @Override
    public boolean isLessThan(double epsilon) {
        return this.val < epsilon;
    }

    @Override
    public boolean isGreaterThan(double epsilon) {
        return this.val > epsilon;
    }

    @Override
    public boolean isEqualTo(double epsilon) {
        return this.val == epsilon;
    }

    @Override
    public void setValue(double val) {
        this.val = (int) val;
    }

    @Override
    public Storage abs() {
        return new IntegerStorage(Math.abs(this.val));
    }
}
